package src.src;
//package src;
import java.util.Iterator;

/**
 * TCSS 343 - Winter 2014
 * Group 6?
 */

/**
 * Represents a heap node for Prim's algorithm
 * 
 * @author Emily Linniman
 * @version March 9, 2014
 *
 */
public class PrimHeapNode implements Comparable {

	/** The label representing the node. */
	private Object nodeLabel;
	
	/** The vertex representing the node in the graph. */
	private Vertex nodeVertex;
	
	/** The position of the node in the heap. */
	private Object heapPosition;
	
	private double data = 0;
	private boolean discovered = false;
	
	/**
	 * Constructs a prim heap node.
	 * 
	 * @param name the label of the node to be created
	 * @param data2 the vertex of the node to be created
	 */
	public PrimHeapNode(Vertex v, double data2, String name) {
		nodeVertex = v;
		heapPosition = 0;
		if(v.incidentEdgeList.size()>0)
		{
		double current_small = (double) ((Edge) v.incidentEdgeList.get(0)).getData();
        int small_index = 0;
		for(int y = 0; y < v.incidentEdgeList.size(); y++)//Edge e :  initialV.getNode().getVertex().incidentEdgeList)
		{
		  if((double)(((Edge) v.incidentEdgeList.get(y)).getData()) < current_small)
		  {
			  current_small = (double)(((Edge) v.incidentEdgeList.get(y)).getData());
			  small_index = y;
		  }
		}
		nodeVertex.setData(current_small);
		System.out.println("data set to " + current_small);
		data =  current_small;
		}

//		Iterator itr = v.incidentEdgeList.iterator();
//		double minData = 0;
//		if(itr.hasNext())
//			minData = (double) ((Edge) itr.next()).getData();
//		if (itr.hasNext()) {
//			while(itr.hasNext()) {
//				if ( (double)((Edge) itr.next()).getData() > (int) minData) {
//					minData = (double) ((Edge) itr.next()).getData();
//				}
//			}
//		}
//		System.out.println(nodeVertex.getData());
	}
	

	/**
	 * Sets node's heap position to reflect the input.
	 * 
	 * @param the_position the position to set the node's heap position to
	 */
	public void setPosition(int the_position) {
		System.out.println("position set to " + the_position);
		heapPosition = the_position;
	}
	
	/** 
	 * Accesses the position of the node in a heap.
	 * 
	 * @return the heap position of the node
	 */
	public Object getPosition() {
		return heapPosition;
	}
	
	/**
	 * Accesses the vertex of the current node.
	 * 
	 * @return the vertex associated with the node
	 */
	public Vertex getVertex() {
		return nodeVertex;
	}
	
	/**
	 * Accesses the label of the current node.
	 * 
	 * @return the label associated with the node
	 */
	public Object getLabel() {
		return nodeLabel;
	}
	
	private double getMin()
	{
		double current_min = -1;
		PrimHeapNode short_path;
	    for(int x = 0; x < nodeVertex.incidentEdgeList.size(); x++)
	    {
	    	if (((Edge)nodeVertex.incidentEdgeList.get(x)).getNode().discovered)
	    	{
	    	  current_min = (double) ((Edge)nodeVertex.incidentEdgeList.get(x)).getData();
	    	  short_path = ((Edge)nodeVertex.incidentEdgeList.get(x)).getNode();
	    	}
	    }
		
		return current_min;
	}
	
	public boolean getDiscovered()
	{
		return discovered;
	}
	
	public void setDiscovered()
	{
		discovered = true;
	}
	

	@Override
	public int compareTo(Object arg0) {
			return (int) (getMin() - ((PrimHeapNode)arg0).getMin());
	}
	
}
